using System;
using ProjectEuler.Core.Helpers;

namespace ProjectEuler.Core
{
    public class Problem5 : IProjectEulerProblem
    {
        //2520 is the smallest number that can be divided by each of the numbers
        //from 1 to 10 without any remainder.

        //What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?
        public int FastSmallestNumberDivisibleByAllNumberFrom1To(int n)
        {
            var result = 1;
            for (int i = 2; i <= n; i++)
            {
                result = MathHelper.LCM(result, i);
            }
            return result;
        }

        public int SlowSmallestNumberDivisibleByAllNumberFrom1To(int n)
        {
            var guess = 0;
            var guessisdivisiblebyall = false;
            while (!guessisdivisiblebyall)
            {
                guess++;
                var doesthisonework = true;
                for (int i = 1; i <= n; i++)
                {
                    doesthisonework = ((guess % i) == 0);
                    if (!doesthisonework) break;
                }
                guessisdivisiblebyall = doesthisonework;
            }
            return guess;            
        }

        public int Number
        {
            get { return 5; }
        }

        public string Description
        {
            get { return "Find the smallest number that is evenly divisible by all of the numbers from 1 to 20"; }
        }

        public string Answer
        {
            get { return FastSmallestNumberDivisibleByAllNumberFrom1To(20).ToString(); }
        }
    }
}